package hp.com.dao.impl;

import hp.com.dao.AddressDao;
import hp.com.dao.BaseDao;
import hp.com.entity.Address;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AddressDaoImpl extends BaseDao implements AddressDao {
    @Override
    public void addAddressToAccount(Address add)  throws Exception{
        String sql= "insert into addresses (account_id,loc_detail,contact_Name,contact_number,country,province,city) values (?,?,?,?,?,?,?)";
        Object[] obj = {add.getAccountId(),add.getLocDetail(),add.getContactName(),add.getContactNumber()," "," "," "};
        this.executeUpdate(sql,obj);
    }

    @Override
    public List<Address> getAddressesById(int id) throws Exception {
        String sql = "select * from addresses where account_id=?";
        ResultSet rs = this.executeQuery(sql,new Object[]{id});
        List<Address> addrList = new ArrayList<>();
        while(rs.next()){
            Address addr = new Address();
            addr.setId(rs.getInt("id"));
            addr.setAccountId(rs.getInt("account_id"));
            addr.setCountry(rs.getString("country"));
            addr.setProvince(rs.getString("province"));
            addr.setCity(rs.getString("city"));
            addr.setLocDetail(rs.getString("loc_detail"));
            addr.setContactNumber(rs.getString("contact_Number"));
            addr.setContactName(rs.getString("contact_Name"));
            addrList.add(addr);
        }
        return addrList;
    }

    @Override
    public Address getAddressByDataId(int id) throws Exception {
        String sql = "select * from addresses where id = ?";
        ResultSet rs = this.executeQuery(sql,new Object[]{id});
        rs.next();
        Address addr = new Address();
        addr.setId(rs.getInt("id"));
        addr.setAccountId(rs.getInt("account_id"));
        addr.setCountry(rs.getString("country"));
        addr.setProvince(rs.getString("province"));
        addr.setCity(rs.getString("city"));
        addr.setLocDetail(rs.getString("loc_detail"));
        addr.setContactName(rs.getString("contact_Name"));
        addr.setContactNumber(rs.getString("contact_Number"));
        return addr;
    }
}
